﻿#include "Camera.h"
#include "Box.h"
#include "ShareComponents.h"
#include "Player.h"


Camera::Camera(int _left,int _right,int _bottom)
{	

	left = _left;
	right = _right - CENTER_H;	
	vp_y = top = TOP;
	bottom = _bottom - CENTER_V;
	if(bottom < 480) bottom = TOP;
	D3DXMatrixIdentity(&this->matrixI4);
	matrixI4._22 = -1;
	vp_x = left;
	_bound = new Box(D3DXVECTOR2(),D3DXVECTOR2(),D3DXVECTOR2());
}

Camera::~Camera(void)
{
}

void Camera::update(D3DXVECTOR2 target)
{
	if (gl_player->_position.x > right - 237 && vp_x < right - 257)
	{		
		vp_x += 2;
		left += 2;
	}
	else
	{
		if(target.x < right)
		{
			vp_x = target.x - CENTER_H;
			if(vp_x < left) vp_x = left;

			if(target.x > left + CENTER_H)
			{		
				left = vp_x;
			}
		}	

		if(top < bottom)
		{
			vp_y = target.y + CENTER_V;
			if(vp_y < top) 
				vp_y = top;

			if(vp_y > top)
			{		
				top = vp_y ;
			}
		}
	}
}

D3DXMATRIX* Camera::getTransformedMatrix()
{
	D3DXMATRIX transfromMatrix;
	D3DXMatrixTranslation(&transfromMatrix,-vp_x,vp_y,0);
	//nhan nguoc
	D3DXMatrixMultiply(&this->transform,&matrixI4,&transfromMatrix);
	return &transform;

}

float Camera::getLeftSide()
{
	return left;
}

float Camera::getRightSide()
{
	return left + LOOK;
}

float Camera::getTop()
{
	return top - TOP;//luu y top la top cua dau camera để lat
}

float Camera::getBottom()
{
	//bottom chi dung de xet xem cam đã đi hết giới hạn màn hình hay chưa?
	return top;//top cua cam hien tai la bottom cua man hinh
}

Box* Camera::getBox()
{
	_bound->_position.x = getLeftSide();
	_bound->_position.y = getTop();
	_bound->_size.x = 510;
	_bound->_size.y = 480;
	return _bound;
}
